package com.shuyuntu.proposal.service;

import com.shuyuntu.proposal.dao.IDelegateDao;
import com.shuyuntu.proposal.dao.IProposalDao;
import com.shuyuntu.proposal.dao.ISecondMotionDao;
import com.shuyuntu.proposal.entity.Delegate;
import com.shuyuntu.proposal.entity.Proposal;
import com.shuyuntu.proposal.entity.SecondMotion;
import com.shuyuntu.proposal.enums.EProposalState;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by limeng on 16-4-17.
 */
@Service("secondMotionService")
public class SecondMotionService {

    /**
     * 根据某个提案的id,分页查询当前提案的附议信息
     *
     * @param proposalId 待查询提案的id
     * @param pageSize 分页大小
     * @param pageNo 分页页数
     * @param rowCount 行数
     * @return 附议信息列表
     */
    @Transactional
    public List<SecondMotion> getSecondMotionByProposalIdWithPagination(
            Integer proposalId, int pageSize, int pageNo, StringBuffer rowCount) {
        Criterion ct1 = Restrictions.eq("targetProposal.id", proposalId);
        List<SecondMotion> list = secondMotionDao.queryPageList(pageNo, pageSize, rowCount, null, ct1);

        return list;
    }

    /**
     * 发布附议
     * 不允许给自己的提案发表附议
     *
     * @param delegateId 发表附议的教师代表的id
     * @param proposalId 被发表附议的提案的id
     * @param content 附议内容
     * @return 0:成功；1：数据库异常；2：不能给自己的提案附议;3:已经发表过附议;4:提案不在接受附议的状态
     */
    @Transactional
    public int publishSecondMotion(String delegateId, Integer proposalId, String content) {
        Proposal proposal = proposalDao.queryById(proposalId);
        if (delegateId.equals(proposal.getAuthor().getId())) {
            return 2;
        }
        if (proposal.getProposalState() != EProposalState.WAIT_SECOND_MOTION) {
            return 4;
        }
        Delegate delegate = delegateDao.queryById(delegateId);
        SecondMotion secondMotion = new SecondMotion();
        secondMotion.setAuthor(delegate);
        secondMotion.setTargetProposal(proposal);
        secondMotion.setContent(content);
        try {
            Integer res = secondMotionDao.insert(secondMotion);
            if (res == null) {
                return 3;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }

        return 0;
    }

    /**
     * 修改附议
     * @param motionId
     * @param content
     * @return 0:成功; 1:数据库异常; 2:提案属于别人; 3:附议不存在; 4:当前提案状态不允许附议被修改
     */
    @Transactional
    public int updateSecondMotion(Integer motionId, String content, String authorId) {
        SecondMotion motion = secondMotionDao.queryById(motionId);
        if (motion == null) {
            return 3;
        }
        if (!motion.getAuthor().getId().equals(authorId)) {
            return 2;
        }
        if (motion.getTargetProposal().getProposalState() != EProposalState.WAIT_SECOND_MOTION) {
            return 4;
        }
        try {
            motion.setContent(content);
            secondMotionDao.update(motion);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 删除某个提案
     * @param motionId 待删除提案附议的id
     * @return 0:删除成功; 1:不存在当前附议; 2:删除过程中数据库异常;3:附议不是当前用户写的
     */
    @Transactional
    public int deleteMotion(int motionId, String authorId) {
        SecondMotion motion = secondMotionDao.queryById(motionId);
        if (motion == null) {
            return 1;
        }
        if (!motion.getAuthor().getId().equals(authorId)) {
            return 3;
        }
        try {
            secondMotionDao.delete(motion);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }
    }

    /*****dependency injection*****/
    @Resource
    ISecondMotionDao secondMotionDao;
    @Resource
    IProposalDao proposalDao;
    @Resource
    IDelegateDao delegateDao;

    public ISecondMotionDao getSecondMotionDao() {
        return secondMotionDao;
    }

    public void setSecondMotionDao(ISecondMotionDao secondMotionDao) {
        this.secondMotionDao = secondMotionDao;
    }

    public IProposalDao getProposalDao() {
        return proposalDao;
    }

    public void setProposalDao(IProposalDao proposalDao) {
        this.proposalDao = proposalDao;
    }

    public IDelegateDao getDelegateDao() {
        return delegateDao;
    }

    public void setDelegateDao(IDelegateDao delegateDao) {
        this.delegateDao = delegateDao;
    }
}
